<?php

namespace StudyBuddy;

/**
 * Class for parsing the SubmittedProfile
 * which is an object representing data of
 * user profile that was sent to the server from
 * the edit profile interface. Usually this is
 * sent via Edit Profile form on the web
 * but can also be submitted by other means
 * in the future like via API
 *
 *
 */
class UploadresourceParser extends StudyBuddyObject {

    /**
     * @var object of type SubmittedProfile
     */
    protected $oSubmitted;
    protected $oResourceDoc;

    public function __construct(Registry $oRegistry) {
        $this->oRegistry = $oRegistry;
    }

    /**
     *
     * Modify values in User object
     * based on SubmittedProfile
     *
     * @param User $oUser
     * @param SubmittedProfile $o
     *
     * @return bool true
     */
    public function save(SubmittedUploadresource $o) {
        $this->oSubmitted = $o;


        $coll = $this->oRegistry->Mongo->RESOURCE_DOCS;
        $coll->ensureIndex(array('url' => 1), array('unique' => true));

        $aData['title'] = $this->getClean($o->getTitle())->substr(0, 60)->valueOf();
        $aData['url'] = $this->getClean($o->getTitle())->toASCII()->makeLinkTitle()->substr(0, 60)->valueOf();
        $aData['description'] = \wordwrap($this->getClean($o->getDescription())->substr(0, 2000)->valueOf(), 50);
        $aData['category'] = $this->getClean($o->getCategory())->substr(0, 60)->valueOf();
        $aData['level'] = $this->getClean($o->getLevel())->substr(0, 60)->valueOf();
        $aData['subject'] = $this->getClean($o->getSubject())->substr(0, 60)->valueOf();
        
        $resource = $o->getUploadedResource();
        $aData['resource_file'] = $this->getClean($resource['file'])->substr(0, 60)->valueOf();
        $aData['resource_size'] = $this->getClean($resource['size'])->substr(0, 60)->valueOf();
        $aData['resource_type'] = $this->getClean($resource['type'])->substr(0, 60)->valueOf();

        $aData['i_reg_ts'] = time();
        $aData['date_reg'] = date('r');
        
        $aData['i_uid'] = $this->oRegistry->Viewer->getUid();
        $aData['username'] = $this->oRegistry->Viewer->getDisplayName();
        $aData['ulink'] = '<a href="' . $this->oRegistry->Viewer->getProfileUrl() . '">' . $this->oRegistry->Viewer->getDisplayName() . '</a>';
        $aData['avtr'] = $this->oRegistry->Viewer->getAvatarSrc();

        $this->oResourceDoc = \StudyBuddy\MongoDoc::factory($this->oRegistry, 'RESOURCE_DOCS', $aData);
        $res = $this->oResourceDoc->save($aData);
        d('$res: ' . $res);
        $url = $this->oRegistry->Ini->SITE_URL . '/r' . $res . '/' . $aData['url'];
//        Responder::redirectToPage($url);

        return true;
    }

    /**
     * Get value of url
     * append 'http://' if url does not appear
     * to be starting with the http prefix
     * @param Utf8String $str
     * @return string
     */
    protected function getUrl(Utf8String $str) {
        $str = $str->trim();
        if ($str->length() < 4) {
            return '';
        }

        $str = $str->substr(0, 250)->trim()->valueOf();
        if ('http' !== \substr($str, 0, 4)) {
            return 'http://' . $str;
        }

        return $str;
    }

    /**
     * Get clean UTF8String object representing
     * trimmed and clean of html tags
     *
     * @param string $string
     * @return object of type UTF8String
     */
    protected function getClean($string) {
        if (empty($string)) {
            return Utf8String::factory('', 'ascii', true);
        }

        return Utf8String::factory($string)->trim()->stripTags();
    }

}
